# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from app01 import models
# 序列化器类
from app01.serializer.serializer_user import UserSerializer
from app01.serializer.serializer_login import LoginSerializer
from app01.serializer.serializer_carmodel import CarModelSerializer
from app01.serializer.serializer_carfactory import CarFactorySerializer
from app01.serializer.serializer_distributor import DistributorSerializer
# 三大自定义认证
from app01.auth.authentication import JwtAuthentication
from app01.auth.permission import MyPermission
from app01.auth.throttling import MyThrottle

# 过滤,分页,排序
from app01.auth.pagination import MyPagination
from rest_framework.filters import OrderingFilter

# 注册接口
class MyReg(GenericAPIView):
    def post(self,request,*args,**kwargs):
        if request.data:
            print(request.data)
            user = UserSerializer(data=request.data)
            if user.is_valid(raise_exception=True):
                return Response({'code':200,'msg':'注册成功'})
            else:
                return Response({'code':101,'msg':'注册失败'})


# 登录接口
class MyLogin(GenericAPIView):
    def post(self, request):
        login_ser = LoginSerializer(data=request.data)
        if login_ser.is_valid(raise_exception=True):
            return Response({'code':200,'msg':'登陆成功',
                             'url':'http://127.0.0.0.1/home/',
                             'token':login_ser.token,
                               },headers={'Access-Control-Allow-Origin':'*'})
            # 拿到校验成功后的token
        else:
            return Response({'code':101,'msg':'用户名或者密码错误'},headers={'Access-Control-Allow-Origin':'*'})

# 功能接口,查看车型
class MyFunction(GenericAPIView):
    queryset = models.CarModel.objects.all()
    serializer_class = CarModelSerializer
    authentication_classes = [JwtAuthentication]
    # permission_classes = []
    # throttle_classes = [MyThrottle]
    filter_backends = [OrderingFilter]
    pagination_class = [MyPagination]
    def get(self, request, *args, **kwargs):
        if not kwargs:
            car_style_queryset = self.get_queryset()
            car_style = self.get_serializer(instance=car_style_queryset,many=True)
        else:
            car_style_queryset = self.get_object()
            car_style = self.get_serializer(instance=car_style_queryset)
        return Response(car_style.data,headers={'Access-Control-Allow-Origin':'*'})

        # 查看所有车型


# 新增，删除车厂
class MyAdminCarFactory(APIView):
    authentication_classes = [JwtAuthentication]
    permission_classes = [MyPermission]
    # throttle_classes = [MyThrottle]
    def post(self, request, *args, **kwargs):
        car_factory = CarFactorySerializer(data=request.data)
        if car_factory.is_valid(raise_exception=True):
            car_factory.save()
            return Response({'status':200,'msg':'新增成功','data':car_factory.data})
        else:
            return Response({'status': 101, 'msg': '新增失败'})

    def delete(self, request, *args, **kwargs):
        models.CarFactory.objects.filter(**kwargs).delete()
        return Response({'status': 200, 'msg': '删除成功'})

# 新增，删除车型
class MyAdminCarModel(APIView):
    authentication_classes = [JwtAuthentication]
    permission_classes = [MyPermission]
    # throttle_classes = [MyThrottle]
    def post(self, request,*args,**kwargs):
        car_model = CarModelSerializer(data=request.data)
        if car_model.is_valid(raise_exception=True):
            car_model.save()
            return Response({'status':200,'msg':'新增正确','data':car_model.data})
        else:
            return Response({'status':101,'msg':'新增失败'})

    def delete(self, request,*args,**kwargs):
        models.CarModel.objects.filter(**kwargs).delete()
        return Response({'status': 200, 'msg': '删除成功'})

# # 新增，删除经销商
class MyAdminDistributor(APIView):
    authentication_classes = [JwtAuthentication]
    permission_classes = [MyPermission]
    # throttle_classes = [MyThrottle]
    def post(self, request,*args,**kwargs):
        distributor = DistributorSerializer(data=request.data)
        if distributor.is_valid(raise_exception=True):
            distributor.save()
            return Response({'status':200,'msg':'新增正确','data':distributor.data})
        else:
            return Response({'status':101,'msg':'新增失败'})
    def delete(self, request,*args,**kwargs):
        models.Distributor.objects.filter(**kwargs).delete()
        return Response({'status':200,'msg':'删除成功'})

